//
//  LYCProductListView.m
//  爱鲜蜂
//
//  Created by 廖易诚 on 16/9/6.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "LYCProductListView.h"
#import "LYCProductCell.h"
#import "MarketMenuModel.h"

@interface LYCProductListView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LYCProductListView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.products.count;
}

/// 设置每一组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *productSection = self.products[section];
    return productSection.count;
}

// 显示每一个cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    
    LYCProductCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if ( !cell) {
        cell = [[LYCProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSArray *productSection = self.products[indexPath.section];
    MarketProductModel *model = productSection[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    MarketMenuModel *model = self.menus[section];
    NSLog(@"title = %@",model.name);
    return model.name;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    NSLog(@"section = %ld",(long)section);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"menuScrollToPosition" object:@(section)];
}

- (void)setProducts:(NSArray *)products {
    _products = products;
    [self reloadData];
}

- (void)setMenus:(NSArray *)menus {
    _menus = menus;
    [self reloadData];
}




@end
